# Resolvers

Let's focus on `resolve` functions usually called just **resolvers**.

With GraphQL-Modules, you implement your resolvers in the same way you do in any other GraphQL implementation.

Modules created by GraphQL-Modules are smart enough to detect incorrect resolvers (that don't match type definitions or extensions for example). It helps you to detect duplicate and incorrect resolvers, or old resolvers that shouldn't be there.

## Resolvers Example

Let's take for example the following schema for `User` and `Query`.

```ts
import { createModule, gql } from 'graphql-modules'

export const myModule = createModule({
  id: 'my-module',
  dirname: __dirname,
  typeDefs: gql`
    type Query {
      user(id: ID!): User
    }

    type User {
      id: ID!
      username: String!
    }
  `,
  resolvers: {
    Query: {
      user(root, { id }) {
        return {
          _id: id,
          username: 'jhon'
        }
      }
    },
    User: {
      id(user) {
        return user._id
      },
      username(user) {
        return user.username
      }
    }
  }
})
```

## Dynamically Load Resolvers Files

If you have many resolvers files in your module, and you wish to load it dynamically, you can use loaders from `@graphql-tools/load-files`!

Start by installing this package:

```sh npm2yarn
npm i @graphql-tools/load-files
```

Next, use it to load your files dynamically:

```ts
import { createModule } from 'graphql-modules'

export const myModule = createModule({
  id: 'my-module',
  dirname: __dirname,
  typeDefs: loadFilesSync(join(__dirname, './typeDefs/*.graphql')),
  resolvers: loadFilesSync(join(__dirname, './resolvers/*.ts'))
})
```

## Resolver Extensions

You can use resolver extensions to extend the functionality of your resolvers to make your modules work with such extensions as [Grafast Plan Resolver](https://grafast.org/grafast/plan-resolvers#specifying-a-field-plan-resolver) or [GraphQL Query Complexity](https://github.com/slicknode/graphql-query-complexity/blob/HEAD/src/estimators/fieldExtensions/README.md).

To use resolver extensions, you can use the `extensions` property in your resolvers.

```ts
import { createModule, gql } from 'graphql-modules'
import { constant } from "grafast";

export const myModule = createModule({
  id: 'my-module',
  dirname: __dirname,
  typeDefs: [
    gql`
      type Query {
        meaningOfLife: Int!
      }
    `
  ],
  resolvers: {
    Query: {
      meaningOfLife: {
        extensions: {
          grafast: {
            plan() {
              return constant(42);
            },
          },
        },
      },
    }
  }
})
```

